package com.example.bgmachine.bgmachine.sets;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;

import java.util.LinkedList;

public class MaterialSetAdapter extends BaseAdapter {

    private Context mContext;
    private LinkedList<MaterialSetData> mData = new LinkedList<MaterialSetData>();

    public MaterialSetAdapter(Context context, LinkedList<MaterialSetData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        MaterialSetAdapter.ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.frament_set_material_list_item_content, null);
            convertViewtms= convertView;

            holder = new MaterialSetAdapter.ViewHolder();
            holder.mMaterialSetData = mData.get(position);
            holder.material_item_pin_textview  = convertView.findViewById(R.id.material_item_pin_textview);
            holder.material_item_name_textview   = convertView.findViewById(R.id.material_item_name_textview);
            holder.frament_material_input_edittext = convertView.findViewById(R.id.frament_material_input_edittext);
            holder.frament_material_full_textview= convertViewtms.findViewById(R.id.frament_material_full_textview);

            holder.frament_material_list_content_bottom_btn_operation_full   = convertView.findViewById(R.id.frament_material_list_content_bottom_btn_operation_full);
            holder.frament_material_list_content_bottom_btn_operation_set   = convertView.findViewById(R.id.frament_material_list_content_bottom_btn_operation_set);
            holder.frament_material_list_content_bottom_btn_operation_set.setTag( holder );
            holder.frament_material_list_content_bottom_btn_operation_set.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder)v.getTag();

                    if(vs == null){
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=" );
                        return;
                    }
                    if(vs.frament_material_input_edittext.getText().length() > 0){
                        vs.mMaterialSetData.setSetInfor(vs.frament_material_input_edittext.getText().toString());
                        HomePages.mHomePages.addAllowance(vs.mMaterialSetData.getMenuid(), Integer.parseInt( vs.frament_material_input_edittext.getText().toString() ) );
                        HomePages.mHomePages.allowanceViewUpdae();
                        HomePages.mHomePages.setParamView();
                    }


                }
            });
            holder.frament_material_list_content_bottom_btn_operation_full.setTag( holder );
            holder.frament_material_list_content_bottom_btn_operation_full.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder)v.getTag();

                    if(vs == null){
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=" );
                        return;
                    }

                    HomePages.mHomePages.updateAllowance(vs.mMaterialSetData.getMenuid(),0);

                    HomePages.mHomePages.allowanceViewUpdae();
                    HomePages.mHomePages.setParamView();
                }
            });

            holder.material_item_pin_textview.setText(String.valueOf( holder.mMaterialSetData.getMenuid()));
            holder.material_item_name_textview.setText( holder.mMaterialSetData.getMenuName() );
            holder.frament_material_input_edittext.setText( holder.mMaterialSetData.getSetInfor() );
            holder.frament_material_full_textview.setText( holder.mMaterialSetData.getFullInfor() );
            convertView.setTag(holder);
        } else {

            holder = (MaterialSetAdapter.ViewHolder) convertViewtms.getTag();
            holder.mMaterialSetData = mData.get(position);
            holder.material_item_pin_textview  = convertViewtms.findViewById(R.id.material_item_pin_textview);
            holder.material_item_name_textview   = convertViewtms.findViewById(R.id.material_item_name_textview);
            holder.frament_material_input_edittext = convertViewtms.findViewById(R.id.frament_material_input_edittext);
            holder.frament_material_full_textview= convertViewtms.findViewById(R.id.frament_material_full_textview);
            holder.frament_material_list_content_bottom_btn_operation_full   = convertViewtms.findViewById(R.id.frament_material_list_content_bottom_btn_operation_full);
            holder.frament_material_list_content_bottom_btn_operation_set   = convertViewtms.findViewById(R.id.frament_material_list_content_bottom_btn_operation_set);
            holder.frament_material_list_content_bottom_btn_operation_set.setTag( holder );
            holder.frament_material_list_content_bottom_btn_operation_full.setTag( holder );
            holder.material_item_pin_textview.setText(String.valueOf( holder.mMaterialSetData.getMenuid()));
            holder.material_item_name_textview.setText( holder.mMaterialSetData.getMenuName() );
            holder.frament_material_input_edittext.setText( holder.mMaterialSetData.getSetInfor() );
            holder.frament_material_full_textview.setText( holder.mMaterialSetData.getFullInfor() );


        }

        return convertViewtms;
    }
    //添加一个元素
    public void add(MaterialSetData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, MaterialSetData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(MaterialSetData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {

        public  TextView material_item_pin_textview = null;
        public  TextView material_item_name_textview  = null;
        public  TextView frament_material_full_textview  = null;


        public EditText frament_material_input_edittext = null;
        public Button frament_material_list_content_bottom_btn_operation_full  = null;
        public Button frament_material_list_content_bottom_btn_operation_set  = null;


        public MaterialSetData mMaterialSetData = null;

    }


}
